<?php
namespace app\home\home;
use \Alipay\Alipay;
use think\Request;

class Alipayapi extends Base
{
    protected $alipay;

    //初始化
    public function __construct()
    {
        $this->alipay = new Alipay();
    }


    /**
     * 支付
     */
    public function ercode($order_info){
        $out_trade_no  = $order_info['out_trade_no'];
        //订单名称，必填
        $proName       = $order_info['subject'];
        //付款金额，必填
        $total_amount  = $order_info['total_amount'];
        $pcaliay = new Ceshialipy();
        $data['out_trade_no']=$out_trade_no;
        $data['subject']=$proName;
        $data['total_amount']=$total_amount;
        $alipyresult = $pcaliay->useAliPay($data);
        echo $alipyresult;
    }




    //二维码查询
    public function queryorder($out_trand_no=''){
        if(empty($out_trand_no)){
            $out_trand_no = input('param.out_trade_no');
        }
        $pcaliay = new Ceshialipy();
        file_put_contents('out_trand.log',$out_trand_no);
        $queryresult = $pcaliay->orderqueryno($out_trand_no);
        file_put_contents('queryorder.log',json_encode($queryresult,true));
        $this->changeOrderinfo($queryresult->out_trade_no,$queryresult->total_amount);
    }


    //APP支付
    public function alipay($order_info)
    {
        $out_trade_no  = $order_info['out_trade_no'];
        //订单名称，必填
        $proName       = $order_info['subject'];
        //付款金额，必填
        $total_amount  = $order_info['total_amount'];
        $orderString = $this->alipay->pay($proName, $out_trade_no, $total_amount);

        $data['url'] = $orderString;
        $data['orderid'] = $out_trade_no;
        $data['type']=1;
        datamsg(1, "获取成功", $data);

    }




    //退款
    public function refund()
    {
        $price = input('param.price'); //价格

        $out_trade_no = input('param.out_trade_no'); //订单号

        $cancel_reason = input('param.cancel_reason'); //退款理由

        if (!$price) apistatus(0, "退款价格不允许为空", []);

        if (!$out_trade_no) apistatus(0, "唯一订单号不允许为空", []);

        if (!$cancel_reason) apistatus(0, "退款理由不允许为空", []);

        $order_type = explode('_', $out_trade_no);

        if ($order_type[0] == "MJS") {

            AlipayService::refund($out_trade_no, $price, $cancel_reason);

        } else {

            apistatus(0, "当前订单号错误或者该订单不允许退款操作");

        }
    }


    //前端支付，同步调用 之前测试
    public function notify1($orderid='')
    {
        if(empty($orderid)) {
            $orderid = input('param.orderid');
        }
        $queryresult=$this->alipay->orderQuery($orderid);
        echo $queryresult;
    }




    //退款查询
    public function refundQuery()
    {

        $out_trade_no = input('param.out_trade_no'); //订单号

        if (!$out_trade_no) apistatus(0, "唯一订单号不允许为空", []);

        return $this->alipay->refundQurey($out_trade_no);

    }


    //转账接口
    public function fundTransToAccount()
    {
        $outBizNo = input('param.outBizNo'); //订单号

        $payeeAccount = input('param.payeeAccount'); //支付宝账号

        $amount = input('param.amount'); //金额

        if (!$outBizNo) apistatus(0, "唯一订单号不允许为空", []);

        if (!$payeeAccount) apistatus(0, "支付宝账号不允许为空", []);

        if (!$amount) apistatus(0, "转账金额不允许为空", []);

        return $this->alipay->fundTransToAccount($outBizNo, $payeeAccount, $amount);

    }





    //异步回调
    public function returnnotify($param=[]){
        if(empty($param)){
            $param = input('param.');
        }
        file_put_contents('alipy.log',json_encode($param));
    }




    //订单查询 RSA2 dz
    public function notify($orderid='')
    {
        if(empty($orderid)) {
            $orderid = input('param.orderid');
        }
        $param = input('param.');
        file_put_contents('b.log',$param);
        $queryresult=$this->alipay->orderQuery($orderid);
        $result = json_decode($queryresult,true);
        if($result['alipay_trade_query_response']['code'] == 10000){
            $changeorder = $this->changeOrderinfo($result['alipay_trade_query_response']['out_trade_no'],$result['alipay_trade_query_response']['total_amount']);//写入订单表
            //如果后台开启了分销的功能，则产生对应的佣金
            if(config('kouzi.feestatus') == 1 ) {
                if ($changeorder) {
                    $viporder = db('home_viporder')->where(['orderid' => $orderid])->find(); //获取当前的订单信息
                    $parentuser = $this->parentIdname($viporder['uid']);
                    $this->cashflow($viporder['uid'], $parentuser, $orderid);
                }
            }
            //分佣结束
        }
    }


    public function ceshi(){
        $uid = input('param.uid');
        $orderid = input('param.orderid');
        $parentuser =$this->parentIdname($uid);
        $this->cashflow($uid,$parentuser,$orderid);
        dump($parentuser);
    }



}